using System;
using System.Windows.Forms;

using Brilliance;
using Brilliance.navigation;

using SampleApplication.model;
using SampleApplication.service;
using SampleApplication.view;

namespace SampleApplication.presenter
{
    [Navigation(Constants.SUCCEED_LOGIN, typeof(SucceedLoginAction))]
    [Navigation(Constants.FAILED_LOGIN, typeof (SucceedLoginAction))]
    public class LoginPresenter : Presenter, ILoginPresenter
    {
        private ILoginView view;
        private Principal principal;

        [Out]
        public Principal Principal
        {
            get { return principal; }
        }

        private PrincipalService principalService;

        public PrincipalService PrincipalService
        {
            get { return principalService; }
            set { principalService = value; }
        }

        public LoginPresenter()
        {
            principal = new Principal();
            principalService = new PrincipalService();
        }

        protected override void PostViewSet(IView view)
        {
            this.view = (ILoginView) view;
            this.view.Login += OnLogin;
            this.view.Reset += OnReset;
            this.view.Principal = principal;
        }

        private void OnLogin(object sender, EventArgs e)
        {
            principal = view.Principal;
            if (principalService.Login(principal))
            {
                OnNavigate(Constants.SUCCEED_LOGIN);
            }
            else
            {
                MessageBox.Show("Please check your username/password:\n" + principal.Username + "\n" + principal.Password);
            }
        }

        private void OnReset(object sender, EventArgs e)
        {
            view.Principal = new Principal();
        }

        public override void Display()
        {
            view.DisplayView();
        }
    }
}